<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

/**
 * 测试控制器 - 用于测试数据库连接和Excel处理功能
 */
class Test extends Controller
{
    /**
     * 测试数据库连接
     */
    public function testDatabase()
    {
        try {
            echo "<h2>数据库连接测试</h2>";

            // 测试基本连接
            $result = Db::query('SELECT 1 as test');
            echo "<p style='color: green;'>✓ 数据库连接成功!</p>";

            // 检查表是否存在
            $tables = [
                'symptom_categories' => '症状分类表',
                'symptoms' => '症状表',
                'formulas' => '汤药表'
            ];

            echo "<h3>表结构检查</h3>";
            foreach ($tables as $table => $desc) {
                try {
                    $count = Db::name($table)->count();
                    echo "<p style='color: green;'>✓ {$desc} ({$table}) 存在，当前记录数: {$count}</p>";
                } catch (\Exception $e) {
                    echo "<p style='color: red;'>✗ {$desc} ({$table}) 不存在或有问题: " . $e->getMessage() . "</p>";
                }
            }

            // 检查"其他症状"分类
            echo "<h3>其他症状分类检查</h3>";
            try {
                $otherCategory = Db::name('symptom_categories')
                    ->where('name_zh_cn', '其他症状')
                    ->find();

                if ($otherCategory) {
                    echo "<p style='color: green;'>✓ 找到'其他症状'分类，ID: {$otherCategory['id']}</p>";
                } else {
                    echo "<p style='color: orange;'>⚠ 未找到'其他症状'分类，将创建一个...</p>";

                    // 创建"其他症状"分类
                    $categoryData = [
                        'name_zh_cn' => '其他症状',
                        'name_en_us' => 'Other Symptoms',
                        'name_ja_jp' => 'その他の症状',
                        'name_be_by' => 'Іншыя сімптомы',
                        'description_zh_cn' => '其他未分类症状',
                        'description_en_us' => 'Other uncategorized symptoms',
                        'description_ja_jp' => 'その他の未分類症状',
                        'description_be_by' => 'Іншыя некласіфікаваныя сімптомы',
                        'sort_order' => 999,
                        'is_active' => 1,
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s')
                    ];

                    $id = Db::name('symptom_categories')->insertGetId($categoryData);
                    echo "<p style='color: green;'>✓ 已创建'其他症状'分类，ID: {$id}</p>";
                }
            } catch (\Exception $e) {
                echo "<p style='color: red;'>✗ 查询'其他症状'分类时出错: " . $e->getMessage() . "</p>";
            }

        } catch (\Exception $e) {
            echo "<p style='color: red;'>数据库连接失败: " . $e->getMessage() . "</p>";
        }
    }

    /**
     * 测试Excel处理功能
     */
    public function testExcelProcessor()
    {
        try {
            echo "<h2>Excel处理功能测试</h2>";

            // 导入Excel处理器
            $processor = new \app\index\controller\ExcelProcessor();

            // 调用处理方法
            $response = $processor->processExcelData();
            $result = json_decode($response->getData(), true);

            if ($result['code'] == 200) {
                $data = $result['data'];
                echo "<h3 style='color: green;'>✓ 处理成功</h3>";
                echo "<p>分类处理数量: {$data['categories_processed']}</p>";
                echo "<p>症状处理数量: {$data['symptoms_processed']}</p>";
                echo "<p>汤药处理数量: {$data['formulas_processed']}</p>";

                if (!empty($data['errors'])) {
                    echo "<h4 style='color: orange;'>警告信息:</h4>";
                    foreach ($data['errors'] as $error) {
                        echo "<p style='color: orange;'>- {$error}</p>";
                    }
                }
            } else {
                echo "<h3 style='color: red;'>✗ 处理失败</h3>";
                echo "<p>{$result['msg']}</p>";
            }

        } catch (\Exception $e) {
            echo "<p style='color: red;'>Excel处理测试失败: " . $e->getMessage() . "</p>";
        }
    }

    /**
     * 显示所有测试
     */
    public function index()
    {
        echo "<!DOCTYPE html>";
        echo "<html lang='zh-CN'>";
        echo "<head><meta charset='UTF-8'><title>Excel处理系统测试</title></head>";
        echo "<body style='font-family: Arial, sans-serif; margin: 40px;'>";
        echo "<h1>Excel处理系统测试</h1>";
        echo "<p><a href='/index/test/testDatabase' style='display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin: 5px;'>测试数据库连接</a></p>";
        echo "<p><a href='/index/test/testExcelProcessor' style='display: inline-block; padding: 10px 20px; background: #28a745; color: white; text-decoration: none; border-radius: 5px; margin: 5px;'>测试Excel处理</a></p>";
        echo "<p><a href='/index/excel_processor/index' style='display: inline-block; padding: 10px 20px; background: #6c757d; color: white; text-decoration: none; border-radius: 5px; margin: 5px;'>Excel处理界面</a></p>";
        echo "</body>";
        echo "</html>";
    }
}